package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractAction;

public class RelationshipWarningAction extends AbstractAction {
  public RelationshipWarningAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.MtmDocumentRelationshipDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return null;
  }
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer,conn);
    
    RelationshipWarningForm relationshipWarningForm = (RelationshipWarningForm)form;

    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    String[] relationshipID = relationshipWarningForm.getRelationshipID();
    String relatedFolderNum = request.getParameter("relatedFolderNum");
    String relatedDocumentNum = request.getParameter("relatedDocumentNum");
    
    List hasRelatedDocument = new ArrayList();
    for (int i=0; i < relationshipID.length; i++){
      Integer id = TextUtility.parseIntegerObj(relationshipID[i]);
      if (!Utility.isEmpty(id)){
        DmsDocument tmpDocument = (DmsDocument)documentDAO.getDocumentByID(id,null);
        if (!Utility.isEmpty(tmpDocument)) hasRelatedDocument.add(tmpDocument);
      }
    }
    request.setAttribute("RelationshipWarningList", hasRelatedDocument);
    
    /*
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    MtmDocumentRelationshipDAObject mtmRelationshipDAO = new MtmDocumentRelationshipDAObject(sessionContainer, conn);
    String[] relationshipID = relationshipWarningForm.getRelationshipID();
    List mtmRelationships = new ArrayList();
    for (int i=0; i<relationshipID.length; i++) {
      Integer id = TextUtility.parseIntegerObj(relationshipID[i]);
      if (id!=null) {
        MtmDocumentRelationship relationship = (MtmDocumentRelationship)mtmRelationshipDAO.getObjectByID(id);
        if (relationship!=null) mtmRelationships.add(relationship);
      }
    }
    //permissionManager.hasAccessRight(conn, GlobalConstant.OBJECT_TYPE_DOCUMENT, )
    request.setAttribute("RelationshipWarningList", mtmRelationships);
    */
    
    return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
  }
}
